package com.shumi.fun;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class DateFun {
	static SimpleDateFormat format4 = new SimpleDateFormat("yyyy");
	static SimpleDateFormat format10 = new SimpleDateFormat("yyyy-MM-dd");
	static SimpleDateFormat format11 = new SimpleDateFormat("yyMMdd_HHmm");
	static SimpleDateFormat format14 = new SimpleDateFormat("yyyyMMddHHmmss");
	static SimpleDateFormat format16 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	static SimpleDateFormat format19 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	static GregorianCalendar gc = new GregorianCalendar();
	
	public static Date addYear(Date d, int len) {
		gc.setTime(d);
		gc.add(GregorianCalendar.YEAR, len);
		return gc.getTime();
	}

	public static Date addMonth(Date d, int len) {
		gc.setTime(d);
		gc.add(GregorianCalendar.MONTH, len);
		return gc.getTime();
	}

	public static Date addDay(Date d, int len) {
		gc.setTime(d);
		gc.add(GregorianCalendar.DATE, len);
		return gc.getTime();
	}

	public static Date addHour(Date d, int len) {
		gc.setTime(d);
		gc.add(GregorianCalendar.HOUR, len);
		return gc.getTime();
	}

	public static Date addMinute(Date d, int len) {
		gc.setTime(d);
		gc.add(GregorianCalendar.MINUTE, len);
		return gc.getTime();
	}

	public static Date addSecond(Date d, int len) {
		gc.setTime(d);
		gc.add(GregorianCalendar.SECOND, len);
		return gc.getTime();
	}

	public static int subDay(Date d1, Date d2) {
		long sub = d1.getTime() - d2.getTime();
		return (int) (sub / 1000 * 3600 * 24);
	}

	public static String getSubCurrent(int len) {
		return format10
				.format(new Date().getTime() + len * 24 * 60 * 60 * 1000);
	}

	public static int getDateLastMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.MONTH) - 1);// 让日期加1
		return calendar.get(Calendar.DATE);
	}

	public static String getCurrentDate(String pattern) {
		return new SimpleDateFormat(pattern).format(new Date());
	}

	public static String getCurrentTime() {
		return new SimpleDateFormat("HH:mm:ss").format(new Date());
	}

	/**
	 * 得到UTC时间，类型为字符串，格式为"yyyy-MM-dd HH:mm" 如果获取失败，返回null
	 * 
	 * @return
	 */
	public static long getUTCTime(String time, String format) {
		Calendar cal = Calendar.getInstance();
		try {
			// 设置时间
			cal.setTime(new SimpleDateFormat(format).parse(time));
			// 2、取得时间偏移量：
			int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
			// 3、取得夏令时差：
			int dstOffset = cal.get(Calendar.DST_OFFSET);
			cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
			return cal.getTimeInMillis();
		} catch (ParseException e) {
			return 0;
		}
	}

	/**
	 * 将UTC时间转换为东八区时间
	 *
	 * @param UTCTime
	 * @return
	 */
	public static String getLocalTimeFromUTC(String UTCTime) {
		Date UTCDate = null;
		String localTimeStr = null;
		try {
			UTCDate = format16.parse(UTCTime);
			format16.setTimeZone(TimeZone.getTimeZone("GMT-8"));
			localTimeStr = format16.format(UTCDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return localTimeStr;
	}

	public static String getCurrent() {
		return format19.format(new Date());
	}
	
	public static String getCurrent11() {
		return format11.format(new Date());
	}
	
	public static String getCurrent14() {
		return format14.format(new Date());
	}

	public static String getCurrentYear() {
		return format4.format(new Date());
	}

	public static String getDateByLong(long timestmp) {
		return format19.format(new Date(timestmp));
	}

	public static Date parseStr19toDate(String str) {
		try {
			return format19.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return null;
		}
	}
}
